package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.EditInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import org.simple.eventbus.Subscriber;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/3/5.
 * 修改用户名
 */

public class ChangeUserNameFragment extends BaseFragment {

    @BindView(R.id.tv_userName)
    TextView tv_userName;
    @BindView(R.id.buttonVerify)
    Button buttonVerify;
    @BindView(R.id.et_verCode)
    EditText et_verCode;

    //输入的短信验证码
    private String userVerify;
    //获取的短信验证码
    private String verifyCode = "";
    private static final int SECONDS = 60;

    private int nowSeconds = SECONDS;

    private static Handler sHandler = new Handler();
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (nowSeconds >= 2) {
                nowSeconds--;
                buttonVerify.setText(nowSeconds + "s后重发");
                sHandler.postDelayed(this, 1000);
            } else {
                nowSeconds = SECONDS;
                buttonVerify.setText("获取验证码");
                buttonVerify.setEnabled(true);
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_user_name;
    }

    @Subscriber(tag = Constants.TAG_SET_USER_PHONE_SUCCESS)
    private void onEdit(EditInfo info) {
        int type = info.getType();
        switch (type) {
            case EditInfo.TYPE_USER_PHONE:
                tv_userName.setText(info.getValue());
                break;

        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        tv_userName.setText(userInfo.getWebzzCmsBuyerInfo().getTelphone());

    }

    @OnClick({R.id.bt_next, R.id.buttonVerify})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_next:
               String message=checkMessage();
                if(TextUtils.isEmpty(message)){
                    UIHelper.showUserNameSec(mActivity);
                }else {
                    toast(message);
                    return;
                }

                break;
            case R.id.buttonVerify:
                doVerify();
                break;
            default:
                break;
        }
    }

    private String checkMessage(){
        String verCode=et_verCode.getText().toString();
        if(TextUtils.isEmpty(verCode)) return "请输入验证码";
        if(!verCode.equals(verifyCode)) return "输入的验证码不正确";
        return "";
    }
    private void doVerify() {
        buttonVerify.setEnabled(false);
        sHandler.post(runnable);
        ApiHttpClient.verify(ChangeUserNameFragment.this, userInfo.getWebzzCmsBuyerInfo().getTelphone(),
                             new JsonCallback<String>(String.class, true) {

                                 @Override
                                 public void onBefore(Request request) {
                                     super.onBefore(request);
                                 }

                                 @Override
                                 public void onError(Call call, Exception e) {
                                     toast("验证码获取失败");
                                     sHandler.removeCallbacks(runnable);
                                     nowSeconds = SECONDS;
                                     buttonVerify.setText("获取验证码");
                                     buttonVerify.setEnabled(true);
                                 }

                                 @Override
                                 public void onResponse(ResponseInfo<String> response) {
                                     int code = response.getCode();
                                     String message = response.getMessage();
                                     if (code == 1) {
                                         showWarnDialog(message);
                                         sHandler.removeCallbacks(runnable);
                                         nowSeconds = SECONDS;
                                         buttonVerify.setText("获取验证码");
                                         buttonVerify.setEnabled(true);
                                         return;
                                     }
                                     Map<String, Object> _map = response.getFirstEntity();
                                     verifyCode = String.valueOf(_map.get("verify"));
                                 }

                                 @Override
                                 public void onAfter() {
                                     super.onAfter();
                                 }
                             });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        sHandler.removeCallbacks(runnable);
    }
}
